/**
 * Created by mapbar_front on 2019/8/27.
 */
import React,{Component} from 'react';

//Promise新建之后就会立即执行

const promise = new Promise((resolve,reject)=>{
    //console.log('promise');
    resolve();
})
promise.then(function(){
    //console.log('resolve');
})
//console.log('Hi');
//执行结果如下：
//promise
//Hi
//resolve

//异步加载图片
function loadImageAsync(url){
    return new Promise((resolve,reject)=>{
        const image = new Image();
        image.onload = function(){
            //console.log('image',image);
            resolve(image);
        }

        image.onerror = function(){
            reject(new Error('Could not load image at ' + url));
        }
        image.src = url;
    })
}
var url = 'https://img-blog.csdnimg.cn/20190827231842588.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JveXNreTAwMTU=,size_16,color_FFFFFF,t_70';
loadImageAsync(url);


//用Promise对象实现的Ajax操作的例子。
const getJSON = function(url){
    const promise = new Promise((resolve,reject)=>{
        const handler = function(){
            if(this.readyState !== 4){
                return;
            }
            if(this.status === 200){
                console.log('this.response',this.response);
                resolve(this.response);
            }else {
                reject(new Error(this.statusText));
            }
        }
        const xhr = new XMLHttpRequest();
        xhr.open("GET",url);
        xhr.onreadystatechange = handler;
        xhr.responseType = "json";
        xhr.setRequestHeader("Accept", "application/json");
        xhr.send();
    });
    return promise;
}
var url =  'http://www.weather.com.cn/data/sk/101010100.html';
getJSON(url).then(res=>{
    //console.log('res',res);
}).catch(error=>{
    console.log('出错了',error);
})


const p1 = new Promise((resolve,reject)=>{

});
//一个异步操纵的结果返回另一个异步操作
const p2 = new Promise((resolve,reject)=>{
    resolve(p1);
})


const pp1 = new Promise((resolve,reject)=>{
    setTimeout(function(){
        //resolve('123');
        reject(new Error('失败！'));
    },3000)
})

const pp2 = new Promise((resolve,reject)=>{
   setTimeout(()=>{
       resolve(pp1);
   },1000)
})

pp2.then(res=>{
    console.log('pp2-res',res);
}).catch(err=>{
    console.log('pp2-err',err);
})


console.log('------------');
new Promise((resolve,reject)=>{
    console.log(1);
    resolve(11);
    console.log(2);
}).then(res=>{
    console.log('res',res);
}).catch(err=>{
    console.log(err);
})

new Promise((resolve,reject)=>{
    return resolve(1);
}).then(res=>{
    console.log(res);
}).catch(err=>{
    console.log(err);
})

console.log('start');
setTimeout(function(){
    console.log('222');
},0)
setTimeout(function(){
    console.log('1000');
},1000)
new Promise((resolve,reject)=>{
    console.log('resolve');
    resolve();
    console.log('333');
})
console.log('444');

//start resolve 333 444 222 1000

for(var i=0;i<3;i++){
    setTimeout(function(){
        console.log('i',i); //3个3
    },0)
    console.log('i-2',i);// 0 1 2
}


console.log(1);

setTimeout(function() {
    console.log(2);
}, 0);

Promise.resolve().then(function() {
    console.log(3);
}).then(function() {
    console.log(4);
});

console.log(5);

//1 5 3 4 2

const promise829 = new Promise((resolve,reject)=>{
    //reject(new Error('有一个错误'));
    //throw new Error('有一个错误');
    resolve('ok');
    try{
        throw new Error('有一个错误');
    }catch(e){
        reject(e);
    }
})
promise829.then(error=>{
    console.log('error',error);
})

// var url =  'http://qwww.weather.com.cn/data/sk/101010100.html';
// getJSON(url).then(res=>{
//     console.log('www-res',res);
// },(error)=>{
//     console.log('wwww-error',error);
// })

const someAsyncThing = new Promise((resolve,reject)=>{
    //resolve(x+2);
})
someAsyncThing.catch(function(error){
    console.log('everything is great',error);
}).then(()=>{
    console.log('data123')
})

setTimeout(()=>{console.log('8-29')},2000)


const promise8291 = new Promise(function(resolve,reject){
    resolve('ok');
    //setTimeout(function(){throw new Error('抛出一个错误')},0)
})
promise8291.then(value=>{
    console.log('promise8291',value);
})

Promise.resolve()
    .catch((err)=>{
        console.log(err);
    }).then(()=>{
    console.log('carry on');
})


const promise8292 = new Promise((resolve,reject)=>{
    //resolve(x+2);
})
promise8292.then()

//var a = (x,y,z)=> x+y+z;

function add(a,b){
    return a+b;
}
add(1,2);

function f(){
    console.log('arguments',arguments);
    let a = Array.prototype.slice.call(arguments);
    console.log('a',a);

    if(arguments.length===1){
        return arguments[0];
    }else if(arguments.length === 2){
        return arguments[0]+arguments[1];
    }

}
f(1,2);

//如果参数未定的话，如何操作？


class Promises extends Component{
    render(){
        return(
            <div>
                Promise
            </div>
        )
    }
}
export default Promises;

